package liumengbaio.lolzhangyoubao.Activity.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.PopupWindow;

import liumengbaio.lolzhangyoubao.Activity.utils.GoodView;
import liumengbaio.lolzhangyoubao.R;

/**
 * 小说阅读界面
 * Created by lanouhn on 16/9/6.
 */
public class MoreNovelBodyActivity extends AppCompatActivity implements View.OnClickListener {

    private WebView mWebView;
    private ImageView video_good, video_goodChecked, video_bad, video_badChecked, more, back;
    private GoodView mGoodView;
    private PopupWindow mPopupWindow;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.video_third_body);
        mGoodView = new GoodView(this);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        mWebView = (WebView) findViewById(R.id.video_third_wb);
        video_good = (ImageView) findViewById(R.id.video_good);
        video_bad = (ImageView) findViewById(R.id.video_bad);
        video_goodChecked = (ImageView) findViewById(R.id.video_goodChecked);
        video_badChecked = (ImageView) findViewById(R.id.video_badChecked);
        more = (ImageView) findViewById(R.id.video_third_badyMore);
        back = (ImageView) findViewById(R.id.video_third_badyBack);
    }

    private void initData() {
        /**
         * 接收从二级页面适配器传过来的小说接口
         */
        Intent intent = getIntent();
        String url = intent.getStringExtra("url");
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.loadUrl(url);
    }

    private void initEvent() {
        video_good.setOnClickListener(this);
        video_bad.setOnClickListener(this);
        video_goodChecked.setOnClickListener(this);
        video_badChecked.setOnClickListener(this);
        more.setOnClickListener(this);
        back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.video_good:
                video_good.setVisibility(View.INVISIBLE);
                video_goodChecked.setVisibility(View.VISIBLE);
                mGoodView.setImage(getResources().getDrawable(R.mipmap.video_third_goodup));
                mGoodView.show(v);
                video_bad.setEnabled(false);
                break;
            case R.id.video_bad:
                video_bad.setVisibility(View.INVISIBLE);
                video_badChecked.setVisibility(View.VISIBLE);
                video_good.setEnabled(false);
                break;
            case R.id.video_goodChecked:
                video_good.setVisibility(View.VISIBLE);
                video_goodChecked.setVisibility(View.INVISIBLE);
                video_bad.setEnabled(true);
                break;
            case R.id.video_badChecked:
                video_bad.setVisibility(View.VISIBLE);
                video_badChecked.setVisibility(View.INVISIBLE);
                video_good.setEnabled(true);
                break;
            case R.id.video_third_badyBack:
                finish();
                break;
            /**
             * popwindow窗口
             */
            case R.id.video_third_badyMore:
                View view = getLayoutInflater().inflate(R.layout.video_popwindow, null);
                mPopupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
                mPopupWindow.showAsDropDown(more);
                view.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (mPopupWindow.isShowing() && mPopupWindow != null) {
                            mPopupWindow.dismiss();
                            mPopupWindow = null;
                        }
                        return false;
                    }
                });
                break;
        }
    }
}
